extends "res://LevelManager.gd"

# 雪山关卡特有属性
@export var blizzard_chance: float = 0.15  # 暴风雪触发几率
@export var blizzard_duration: float = 12.0  # 暴风雪持续时间
@export var blizzard_damage: int = 2  # 暴风雪每秒伤害
@export var avalanche_chance: float = 0.08  # 雪崩触发几率
@export var avalanche_damage: int = 25  # 雪崩伤害
@export var ice_patch_chance: float = 0.1  # 冰面生成几率
@export var ice_patch_duration: float = 15.0  # 冰面持续时间

# 天气计时器
var weather_timer: float = 0.0
var is_blizzard_active: bool = false
var blizzard_active_timer: float = 0.0

# 冰面引用
var ice_patches = []

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置关卡类型
	current_level_type = LevelType.SNOW
	
	# 根据难度调整参数
	adjust_difficulty()
	
	# 添加到关卡管理器组
	add_to_group("level_manager")
	
	# 设置背景
	var background_node = get_node_or_null("Background")
	if background_node:
		background_node.texture = load("res://asset/background/snowmountain/image_fx_.jpg")
	
	# 初始化敌人生成器
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		enemy_spawner_node.level_type = LevelType.SNOW
		enemy_spawner_node.difficulty = level_difficulty
		enemy_spawner_node.start_spawning()
	
	# 显示关卡信息
	print("雪山关卡 - 难度:", level_difficulty)

func _process(delta):
	# 调用父类的_process方法
	super._process(delta)
	
	# 处理天气逻辑
	if not is_blizzard_active:
		weather_timer += delta
		if weather_timer >= 35.0:  # 每35秒检查一次
			weather_timer = 0.0
			if randf() < blizzard_chance:
				trigger_blizzard()
	else:
		blizzard_active_timer += delta
		
		# 暴风雪伤害
		if int(blizzard_active_timer) > int(blizzard_active_timer - delta):  # 每秒触发一次
			if player and is_instance_valid(player):
				player.take_damage(blizzard_damage)
				
				# 减速效果
				if player.has_method("apply_slow_effect"):
					player.apply_slow_effect(0.7, 1.5)
		
		# 暴风雪结束
		if blizzard_active_timer >= blizzard_duration:
			end_blizzard()
	
	# 处理雪崩逻辑
	if randf() < avalanche_chance * delta:
		trigger_avalanche()
	
	# 处理冰面生成逻辑
	if randf() < ice_patch_chance * delta:
		spawn_ice_patch()
	
	# 清理已销毁的冰面
	var active_ice_patches = []
	for ice_patch in ice_patches:
		if is_instance_valid(ice_patch):
			active_ice_patches.append(ice_patch)
	ice_patches = active_ice_patches

func adjust_difficulty():
	# 根据难度调整参数
	match level_difficulty:
		1:
			level_time_limit = 180.0  # 3分钟
			blizzard_chance = 0.1
			blizzard_damage = 1
			avalanche_chance = 0.05
		2:
			level_time_limit = 210.0  # 3.5分钟
			blizzard_chance = 0.15
			blizzard_damage = 1
			avalanche_chance = 0.06
		3:
			level_time_limit = 240.0  # 4分钟
			blizzard_chance = 0.15
			blizzard_damage = 2
			avalanche_chance = 0.07
		4:
			level_time_limit = 270.0  # 4.5分钟
			blizzard_chance = 0.2
			blizzard_damage = 2
			avalanche_chance = 0.08
		5:
			level_time_limit = 300.0  # 5分钟
			blizzard_chance = 0.25
			blizzard_damage = 3
			avalanche_chance = 0.1

func trigger_blizzard():
	is_blizzard_active = true
	blizzard_active_timer = 0.0
	
	# 创建暴风雪效果
	var blizzard = preload("res://BlizzardEffect.tscn").instantiate()
	add_child(blizzard)
	blizzard.global_position = Vector2(get_viewport_rect().size.x / 2, get_viewport_rect().size.y / 2)
	blizzard.duration = blizzard_duration
	blizzard.damage_per_second = blizzard_damage
	
	# 通知UI
	if ui:
		ui.show_message("暴风雪来袭！")
	
	print("暴风雪开始!")

func end_blizzard():
	is_blizzard_active = false
	
	# 通知UI
	if ui:
		ui.show_message("暴风雪结束")
	
	print("暴风雪结束!")

func trigger_avalanche():
	# 选择一侧触发雪崩
	var side = randi() % 4  # 0=上, 1=右, 2=下, 3=左
	var viewport_size = get_viewport_rect().size
	var start_position = Vector2.ZERO
	var direction = Vector2.ZERO
	
	match side:
		0:  # 上方
			start_position = Vector2(randf_range(0, viewport_size.x), 0)
			direction = Vector2(0, 1)
		1:  # 右方
			start_position = Vector2(viewport_size.x, randf_range(0, viewport_size.y))
			direction = Vector2(-1, 0)
		2:  # 下方
			start_position = Vector2(randf_range(0, viewport_size.x), viewport_size.y)
			direction = Vector2(0, -1)
		3:  # 左方
			start_position = Vector2(0, randf_range(0, viewport_size.y))
			direction = Vector2(1, 0)
	
	# 创建雪崩效果
	var avalanche = preload("res://AvalancheEffect.tscn").instantiate()
	add_child(avalanche)
	avalanche.global_position = start_position
	avalanche.direction = direction
	avalanche.damage = avalanche_damage
	
	# 通知UI
	if ui:
		ui.show_message("雪崩！小心！")
	
	print("雪崩触发!")

func spawn_ice_patch():
	# 创建冰面
	var ice_patch = preload("res://IcePatchEffect.tscn").instantiate()
	add_child(ice_patch)
	
	# 设置冰面位置（随机位置，但不要太靠近边缘）
	var viewport_size = get_viewport_rect().size
	var x = randf_range(100, viewport_size.x - 100)
	var y = randf_range(100, viewport_size.y - 100)
	ice_patch.global_position = Vector2(x, y)
	
	# 设置冰面属性
	ice_patch.duration = ice_patch_duration
	
	# 添加到冰面列表
	ice_patches.append(ice_patch)
	
	print("冰面出现!")
	
	# 设置冰面消失计时器
	var timer = get_tree().create_timer(ice_patch_duration)
	timer.connect("timeout", _on_ice_patch_timeout.bind(ice_patch))

func _on_ice_patch_timeout(ice_patch):
	if ice_patch and is_instance_valid(ice_patch):
		ice_patch.queue_free()
		
		print("冰面消失!")

# 重写完成关卡方法，添加雪山特有的完成条件
func complete_level():
	# 检查是否击败了足够的敌人
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		var enemies_defeated = enemy_spawner_node.enemies_defeated
		var required_defeats = 70 + (level_difficulty * 15)  # 根据难度调整所需击败敌人数
		
		if enemies_defeated >= required_defeats:
			# 调用父类的完成方法
			super.complete_level()
		elif level_timer >= level_time_limit:
			# 时间到但没有击败足够的敌人
			fail_level()
	else:
		# 如果没有敌人生成器，使用默认完成逻辑
		super.complete_level()
